package com.example.demo.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.pojo.User;
import com.example.demo.pojo.security.UserSecurity;
import com.example.demo.service.impl.UserServiceImpl;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 *重写security认证用户实现
 */
@Service
public class MyUserDetailsService implements UserDetailsService {
    @Resource
    private UserServiceImpl userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,username);
        User user = userService.getOne(queryWrapper);
        //不存在用户
        if (user==null){
            throw new UsernameNotFoundException("找不到该账户信息！");
        }
        //返回用户信息
        return new UserSecurity(user,new ArrayList<>());
    }

}
